using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Smo = Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using SqlWebAdmin.CSharp;

public partial class ServerProperties : System.Web.UI.UserControl
{
    private bool collapsed;
    protected string collapseButtonImageUrl = "~/App_Themes/default/images/icons/16/chevron_drk_up_16.gif";
	protected string expandButtonImageUrl = "~/App_Themes/default/images/icons/16/chevron_drk_down_16.gif";
    private string title = "Undefined";

    public bool IsCollapsed
    {
        get
        {
            if (ViewState["ServerBoxCollapsed"] == null)
            {
                ViewState["ServerBoxCollapsed"] = !BoxContent.Visible;
            }

            return (bool)ViewState["ServerBoxCollapsed"];
        }
        set
        {
            ViewState["ServerBoxCollapsed"] = value;
        }
    }

    public string CollapseButtonImageUrl
    {
        get { return this.collapseButtonImageUrl; }
        set { this.collapseButtonImageUrl = value; }
    }

    public string ExpandButtonImageUrl
    {
        get { return this.expandButtonImageUrl; }
        set { this.expandButtonImageUrl = value; }
    }

    public string Title
    {
        get { return this.title; }
        set { this.title = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        this.TitleLiteral.Text = Title;

        //this.CollapseExpandButton.Click += new ImageClickEventHandler(CollapseExpandButton_Click);
        //this.CollapseExpandButton.ImageUrl = (IsCollapsed) ? expandButtonImageUrl : collapseButtonImageUrl;

        RenderContents();

        this.BoxContent.Visible = !IsCollapsed;


    }

    private void RenderContents()
    {

		Smo.Server smoServer = SmoHelper.GetServer();
		
		
		
        // User Name
		if (smoServer.ConnectionContext.TrueLogin != null)
        {
            Literal userLiteral = new Literal();

			userLiteral.Text = "<div class=\"text\"><b>User:</b>&nbsp;&nbsp;" + smoServer.ConnectionContext.TrueLogin + "</div>";
            BoxContent.Controls.Add(userLiteral);
        }

        // Server Name
		if (smoServer.ConnectionContext.TrueName != null)
        {
            

			Label aspLabel = new Label();
			if(smoServer.ConnectionContext.TrueName.Length > 15)
				aspLabel.Text = "<div class=\"text\"><b>Server:</b>&nbsp;&nbsp;" + smoServer.ConnectionContext.TrueName.Substring(0, 15) + "</div>"; 
			else
				aspLabel.Text = "<div class=\"text\"><b>Server:</b>&nbsp;&nbsp;" + smoServer.ConnectionContext.TrueName + "</div>"; 

			aspLabel.ToolTip = String.Format("Server: {0}", smoServer.ConnectionContext.TrueName);
			BoxContent.Controls.Add(aspLabel);
        }

        // Database Name
        if (Request.QueryString["database"] != null)
        {
            Literal databaseLiteral = new Literal();

            databaseLiteral.Text = "<div class=\"text\"><b>Database:</b>&nbsp;&nbsp;" + Request.QueryString["database"] + "</div>";
            BoxContent.Controls.Add(databaseLiteral);
        }

        // Table Name
        if (Request.QueryString["table"] != null)
        {
            Literal tableLiteral = new Literal();

            tableLiteral.Text = "<div class=\"text\"><b>Table:</b>&nbsp;&nbsp;" + Request.QueryString["table"] + "</div>";
            BoxContent.Controls.Add(tableLiteral);
        }


    }

    protected void CollapseExpandButton_Click(object sender, ImageClickEventArgs e)
    {

        IsCollapsed = !IsCollapsed;
        //this.CollapseExpandButton.ImageUrl = (IsCollapsed) ? expandButtonImageUrl : collapseButtonImageUrl;
        this.BoxContent.Visible = !IsCollapsed;

    }

}
